/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.fn;
 
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable; 
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.write.WritableCellFormat;
import jxl.write.WritableWorkbook;
import toesp.util.JxlUtil;
/**
 *
 * @author satria
 */
public class commonfn {
    private JxlUtil objJxl = new JxlUtil();
    
    public void setCenterJDialog(JDialog objDialog){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        int x = (screenSize.width - objDialog.getWidth()) / 2;
        int y = (screenSize.height - objDialog.getHeight()) / 2;  
        objDialog.setLocation(x, y); 
    }
    
     public void setCenterJPanel(JPanel objPanel){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        int x = (screenSize.width - objPanel.getWidth()) / 2;
        int y = (screenSize.height - objPanel.getHeight()) / 2; 
        objPanel.setBounds(0, 0, screenSize.width, screenSize.height); 
//        objPanel.setLocation(x, y); 
    }
    
    public void exportToExcel(JTable jtable,String[] strHeaderExcel,String[] strExcel,String[] strTitleGrid,Object[][] objData){
        /*--
         * Create workbook and worksheet with default settings
         * strExcel[0] -> nama file
         * strExcel[1] -> nama sheet 
         * strHeaderExcel[0] -> nama header 1
         * strHeaderExcel[1] -> nama header 2
         * strHeaderExcel[2] -> nama header 3
        */
        
        int intLebar = strHeaderExcel.length;
        
        WritableWorkbook workBook = objJxl.createWorkBook(strExcel[0], strExcel[1]);
        objJxl.mergeCells(workBook.getSheet(strExcel[1]), 0, 1, 2, 1);
        objJxl.mergeCells(workBook.getSheet(strExcel[1]), 0, 2, 2, 2);
        objJxl.mergeCells(workBook.getSheet(strExcel[1]), 0, 3, 2, 3);
        
        WritableCellFormat centerAlignedAllBoldformattedCell = objJxl.createFormattedCell(12, null, false, false, null, null, null, Alignment.CENTRE);
        WritableCellFormat rightAlignedAllBoldformattedCell = objJxl.createFormattedCell(11, null, false, false, null, null, null, Alignment.RIGHT);
        WritableCellFormat leftAlignedAllBoldformattedCell = objJxl.createFormattedCell(11, null, false, false, null, null, null, Alignment.LEFT);
        WritableCellFormat bottomBoldFormatedCell = objJxl.createFormattedCell(6, null, false, false, null, Border.BOTTOM,null);
    
        objJxl.addCellToSheet(0, 1, strHeaderExcel[0], centerAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));
        objJxl.addCellToSheet(0, 2, strHeaderExcel[1], centerAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));
        objJxl.addCellToSheet(0, 3, strHeaderExcel[2], centerAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));
     
        //-- Kolom baris
        //-- Column 1,2,3,4,5 
        objJxl.mergeCells(workBook.getSheet(strExcel[1]), 1, 4, 2, 4);
        objJxl.addCellToSheet(0, 4, "Kode Jurusan", centerAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));
        objJxl.addCellToSheet(1, 4, "Jurusan", centerAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));
//        // DATA
       int intRow = 5;
       for (int i = 0; i < objData.length; i++) {
           objJxl.mergeCells(workBook.getSheet(strExcel[1]), 1, intRow, 2, intRow);
           for (int j = 0; j < objData[i].length; j++) {
                String strData = String.valueOf(objData[i][j]);
                objJxl.addCellToSheet(j, intRow, strData, leftAlignedAllBoldformattedCell, workBook.getSheet(strExcel[1]));               
           } 
           intRow++;
       }
  
        objJxl.flush(workBook);
    }
}
